package com.example.rentingapp.fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.activity.OrderDetailsActivity1;
import com.example.rentingapp.activity.OrderDetailsActivity2;
import com.example.rentingapp.adapter.OrderAdapter;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.OrdersEntity;
import com.example.rentingapp.enetity.OrdersResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Tenant_Order_Fragment extends BaseFragment {
    private OrderAdapter orderAdapter = null;
    private ListView listView;
    private List<OrdersEntity> mOrdersEntity = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    orderAdapter = new OrderAdapter(mOrdersEntity, getActivity());
                    listView.setAdapter(orderAdapter);
                    orderAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.fragment_tenant_order;
    }

    @Override
    protected void initView() {
        listView = mRootView.findViewById(R.id.listview2);
    }

    @Override
    protected void initData() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                removeByKey("orders_id");
                String orders_id = String.valueOf(mOrdersEntity.get(position).getOrders_id());
                insertVal("orders_id", orders_id);//将订单id存入本地

                transfer(OrderDetailsActivity2.class, (int) orderAdapter.getItemId(position));
                getActivity().finish();
            }
        });

        findOrderByUserId();//作为房东,通过orders_landlord_id查找订单
    }

    //作为房客,通过orders_tenant_id查找订单
    private void findOrderByUserId() {
        String userId = findByKey("user_id");
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orders_tenant_id", userId);
        Api.config(ApiConfig.FIND_ORDERS_BY_TENANT_ID, params).postRequest(getActivity(), new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                OrdersResponse orderResponse = gson.fromJson(res, OrdersResponse.class);
                if (orderResponse.getSuccess() == true) {
                    List<OrdersEntity> list = orderResponse.getDatas();
                    if (list != null && list.size() > 0) {
                        mOrdersEntity = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("你还未租房,暂无房客订单");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}
